<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Date_input{

        public function __construct(){

            $this->CI = &get_instance();

        }

        public function clean_string($string)
        {
            $string = $this->_replace_delimit($string);
            $string = $this->_clear_last($string);
            $string = $this->_clear_first($string);
            return $string;
        }

        // replace date delimiters with "."
        private function _replace_delimit($string)
        {
            $string = str_replace('-','.',$string);
            $string = str_replace('/','.',$string);
            return $string;
        }

        // remove delimiter from end if exist
        private function _clear_last($string)
        {
            $last = $string[strlen($string)-1];
            if($last=='.')
                $string = substr($string,0,-1);
            return $string;
        }

        // remove delimiter from beginig if exist
        private function _clear_first($string)
        {
            $first = substr($string,0,1);  
            if($first=='.')
                $string = substr($string,1,strlen($string));
            return $string;
        }

        // check date parts number
        // fix onlly numeric parts
        // return string if OK
        // return FALSE if fails
        public function valid_day_month($string)
        {
            // clean string
            //$string = $this->clean_string($string);

            // check date parts number
            $parts = explode('.',$string);
            if(count($parts)!=2)
            {
                return FALSE;
            }

            // first we check day
            // rules : must be diferent then 0 parse(int)
            //       : must be greater then 0 and lower then 32
            $day = (int)$this->_fix_is_numeric($parts[0]);
            if($day<=0 || $day>31){
                return FALSE;
            }

            // second we check month
            // rules : must be diferent then 0 parse(int)
            //       : must be greater then 0 and lower then 13
            $month = (int)$this->_fix_is_numeric($parts[1]);
            if($month<=0 || $month>12){
                return FALSE;
            }

            // return glues date string
            return implode(".",array($day,$month));
        }

        // fix only numeric in string
        // here we expect string is two part with delimiter
        // here we expect delimiter is "."
        public function _fix_is_numeric($string)
        {

            return preg_replace('/[^0-9]/', '', $string);

        }

        // fix year
        public function fix_year($start,$end)
        {
            $start_cor = array();
            $end_cor = array();

            define("Y1",'.1970');
            define("Y2",'.1971');

            $no_post = count($start);
            for($i=0;$i<$no_post;$i++):

                $from = strtotime($this->clean_string($start[$i]).Y1);
                $to = strtotime($this->clean_string($end[$i]).Y1);

                // if end date is bigger than start, incrise end year
                if($from>$to)
                {
                    $to = strtotime($this->clean_string($end[$i]).Y2);
                }

                $start_cor[] = date('d.m.Y',$from);
                $end_cor[] = date('d.m.Y',$to);

                endfor;  
            return array($start_cor,$end_cor);
        }

        // validate and fix input date array
        // if only one element is not valid it return FALSE
        function fix_input_date($date_array)
        {

            $count = count($date_array);

            for($i=0;$i<$count;$i++){

                // clean string
                $date_array[$i] = $this->clean_string($date_array[$i]);

                // validate day/month
                $date_array[$i] = $this->valid_day_month($date_array[$i]);

                if($date_array[$i] == FALSE)
                {
                    return FALSE;
                }

            }

            return $date_array;

        }

    }  
?>
